﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Snake;
using Snake.Net;
using static Microsoft.VisualBasic.Interaction;

namespace BarrierGenarator
{
    public partial class Frm_Login : Form
    {
        public Frm_Login()
        {
            InitializeComponent();
            tbx_pwd.UseSystemPasswordChar = false;
            tbx_username.Text = GetSetting("Snake", "user", "id", "");
            tbx_pwd.Text = GetSetting("Snake", "user", "pw", "");
        }

        private void btn_login_Click(object sender, EventArgs e)
        {
            if(string.IsNullOrWhiteSpace( tbx_username.Text)||string.IsNullOrWhiteSpace(tbx_pwd.Text))
            {
                MessageBox.Show("用户名和密码不能为空!");
                return;
            }
            var result = HttpRequest.Login(tbx_username.Text, tbx_pwd.Text);
            if(result=="OK")
            {
                Hide();
                SaveSetting("Snake", "user", "id", tbx_username.Text);
                SaveSetting("Snake", "user", "pw", tbx_pwd.Text);
                new Frm_Barrier(tbx_username.Text).ShowDialog();
                Environment.Exit(0);
            }
            else
            {
                MessageBox.Show(result);
            }
        }

        

        private void btn_close_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void btn_min_Click(object sender, EventArgs e)
        {
            WindowState = FormWindowState.Minimized;
        }

        private void lab_regist_Click(object sender, EventArgs e)
        {
            new Frm_Register().ShowDialog();
        }

        [DllImport("user32.dll")]
        public static extern bool ReleaseCapture();
        [DllImport("user32.dll")]
        public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
        public const int WM_SYSCOMMAND = 0x0112;
        public const int SC_MOVE = 0xF010;
        public const int HTCAPTION = 0x0002;

        private void Frm_Login_MouseDown(object sender, MouseEventArgs e)
        {
            ReleaseCapture();
            SendMessage(this.Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);
        }
        protected override void OnShown(EventArgs e)
        {
            Graphics g = CreateGraphics();
            g.DrawRectangle(new Pen(Brushes.White, 3f), 0, 0, Width - 3, Height - 3);
        }
    }
}
